<?php

require_once("./lib/pluginbase.php");
require_once("./cfg/item_data.php");

class AdminPlugin extends PluginBase
{
	function processChatCommand($name, $cmd, $args)
	{
		global $item_data;
		global $item_names;
		if(!$this->isAdmin($name))
			return;
		switch($cmd)
		{
			// some aliases for !admin
			case "!cmd":
			case "!a":
			case "!admin":
				fwrite(STDERR, "Running: " . implode(" ", $args) . "\n");
				echo implode(" ", $args) . "\n";
				break;
			case "!spew":
				$player = $name;
				if(count($args) < 1)
				{
					echo "say Usage: !spew id (quantity) (player)\n";
					break;
				}
				$item = $args[0];
				if(!is_numeric($args[0]))
				{
					if (isset($item_names[strtolower($args[0])]))
					{
						$item = $item_names[strtolower($args[0])];
					}
					else
					{
						echo "say Item not regonized, also you're fat and nobody likes you.\n";
						break;
					}
				}
				$qty = 64;
				if(isset($item_data[$item]) && isset($item_data[$item]["defaultspawn"]))
				{
					$qty = $item_data[$item]["defaultspawn"];
				}
				
				if(count($args) == 2)
				{
					if (is_numeric($args[1]))
					{
						$qty = $args[1];
					}
					else
					{
						$player = $args[1];
					}
				}
				if (count($args) > 2)
				{
					if (is_numeric($args[1]))
					{
						$qty = $args[1];
					}
					$player = $args[2];
				}
				if (isset($item_data[$item]) && isset($item_data[$item]["name"]))
				{
					echo "say Spewing ". $item_data[$item]["name"] . " out of " . $player . "\n";
				}
				else
				{
					echo "say Spewing out of " . $player . "\n";
				}
				
				$this->give($player, $item, $qty);
				break;
		}
	}
}

registerPlugin("admin", new AdminPlugin);


